﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Drawing;
using VietKinsNutrition.Data;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid;

namespace VietKinsNutrition.Bussiness
{
    public class ClsTblDm_SosanhnangluonBLL
    {
        private int idPhanLoai;

        ClsTblDm_SosanhnangluonDAL ClsTblDm_Sosanhnangluon_dal = new ClsTblDm_SosanhnangluonDAL();
        public DataTable GetTableNhucaudinhduongotruong(string ProName, int KhoilopId)
        {
            return this.ClsTblDm_Sosanhnangluon_dal.NCDDOTruong(QueryString.PRCTBLDM_NHUCAUDINHDUONGOTRUONG_GETBYKHOILOPID, KhoilopId);
        }
        // fill Nhu cầu ở trường đã nhân với tổng số trẻ ăn.
        private void FillGrid(GridView gridview, DataTable table, string tenLop, string tenBuaAn, int tongSoTre)
        {
            // kiem tra xem bua chon 
            // truong hop ko chon thi tinh ca

            if (gridview.RowCount == 0)
            {
                DataTable tblTyle = new DataTable();
                tblTyle.Columns.Add("tyle");
                tblTyle.Columns.Add("Trenlop");
                tblTyle.Columns.Add("Kcalootruong");
                tblTyle.Columns.Add("ProteinOtruong");
                tblTyle.Columns.Add("Lipidotruong");
                tblTyle.Columns.Add("GlucidOtruong");
                tblTyle.Rows.Add(1);
                tblTyle.Rows.Add(2);
                tblTyle.Rows.Add(3);
                tblTyle.Rows.Add(4);
                gridview.GridControl.DataSource = tblTyle;
                //((DataTable)gridview.GridControl.DataSource).Rows.Add(4); //AddNewRow();
                //gridview.AddNewRow();
                //gridview.AddNewRow();
                //gridview.AddNewRow();//Rows.Add(4);
            }
            int i = 0;

            // truong hop chon bua

            foreach (DataRow dr in table.Rows)
            {
                gridview.SetRowCellValue(i, "tyle", "N.cầu");
                // float phanBoTienAn = GetBuaAn(tenBuaAn);
                float phanBoTienAn = 100;
                gridview.SetRowCellValue(i, "Trenlop", tenLop);
                gridview.SetRowCellValue(i, "Kcalootruong", ((float.Parse(dr["caloCd"].ToString()) * phanBoTienAn) / 100) * tongSoTre);
                gridview.SetRowCellValue(i, "ProteinOtruong", ((float.Parse(dr["PCd"].ToString()) * phanBoTienAn) / 100) * tongSoTre);
                gridview.SetRowCellValue(i, "Lipidotruong", ((float.Parse(dr["LCd"].ToString()) * phanBoTienAn) / 100) * tongSoTre);
                gridview.SetRowCellValue(i, "GlucidOtruong", ((float.Parse(dr["GCd"].ToString()) * phanBoTienAn) / 100) * tongSoTre);
                i = i + 1;
            }
        }
        private void FillGrid_AllSchool(GridView gridview, DataTable[] Arrtable, string tenLop, string tenBuaAn, int[] ArrtongSoTre)
        {
            // kiem tra xem bua chon 
            // truong hop ko chon thi tinh ca
            if (gridview.RowCount == 0)
            {
                gridview.AddNewRow();// Rows.Add(4);
                gridview.AddNewRow();
                gridview.AddNewRow();
                gridview.AddNewRow();
            }
            int i = 0;
            int a = -1;
            // truong hop chon bua
            gridview.SetRowCellValue(0, "Kcalootruong", 0);
            gridview.SetRowCellValue(0, "ProteinOtruong", 0);
            gridview.SetRowCellValue(0, "Lipidotruong", 0);
            gridview.SetRowCellValue(0, "GlucidOtruong", 0);
            float fKcalo = 0;
            float fPro = 0;
            float fLip = 0;
            float fGlu = 0;
            foreach (DataTable table in Arrtable)
            {
                i = 0;
                a = a + 1;
                int tongSoTre = ArrtongSoTre[a];
                foreach (DataRow dr in table.Rows)
                {
                    gridview.SetRowCellValue(0, "tyle", "N.cầu");
                    // float phanBoTienAn = GetBuaAn(tenBuaAn);
                    float phanBoTienAn = 100;
                    gridview.SetRowCellValue(0, "Trenlop", "Tất cả");
                    try
                    {
                        fKcalo = fKcalo + float.Parse(gridview.GetRowCellValue(0, "Kcalootruong").ToString()) + ((float.Parse(dr["caloCd"].ToString()) * phanBoTienAn) / 100) * tongSoTre;
                    }
                    catch
                    {
                    }
                    try
                    {
                        fPro = fPro + float.Parse(gridview.GetRowCellValue(0, "ProteinOtruong").ToString()) + ((float.Parse(dr["PCd"].ToString()) * phanBoTienAn) / 100) * tongSoTre;
                    }
                    catch
                    {
                    }
                    try
                    {
                        fLip = fLip + float.Parse(gridview.GetRowCellValue(0, "Lipidotruong").ToString()) + ((float.Parse(dr["LCd"].ToString()) * phanBoTienAn) / 100) * tongSoTre;
                    }
                    catch
                    {
                    }
                    try
                    {
                        fGlu = fGlu + float.Parse(gridview.GetRowCellValue(0, "GlucidOtruong").ToString()) + ((float.Parse(dr["GCd"].ToString()) * phanBoTienAn) / 100) * tongSoTre;
                    }
                    catch
                    {
                    }
                    i = i + 1;
                }
            }
            gridview.SetRowCellValue(0, "Kcalootruong", fKcalo);
            gridview.SetRowCellValue(0, "ProteinOtruong", fPro);
            gridview.SetRowCellValue(0, "GlucidOtruong", fLip);
            gridview.SetRowCellValue(0, "Lipidotruong", fGlu);
        }
        public void CalculatorTPDD(int KhoilopId, string Tenkhoilop, int tongSoTre, GridView gridViewThucPham, GridView gridViewHienThiPLG, string tenBuaAn)
        {
            DataTable dtNCDD;
            dtNCDD = ClsTblDm_Sosanhnangluon_dal.NCDDOTruong(QueryString.PRCTBLDM_NHUCAUDINHDUONGOTRUONG_GETBYKHOILOPID, KhoilopId);

            this.FillGrid(gridViewHienThiPLG, dtNCDD, Tenkhoilop, tenBuaAn, tongSoTre);
            this.FillGridView(gridViewThucPham, gridViewHienThiPLG);
        }
        public void CalculatorTPDD_AllSchool(int[] ArrKhoilopId, string Tenkhoilop, int[] tongSoTre, GridView gridViewThucPham, GridView gridViewHienThiPLG, string tenBuaAn)
        {
            int i = -1;
            DataTable[] dtNCDD = new DataTable[ArrKhoilopId.Length];
            foreach (int KhoilopId in ArrKhoilopId)
            {
                i = i + 1;
                dtNCDD[i] = ClsTblDm_Sosanhnangluon_dal.NCDDOTruong(QueryString.PRCTBLDM_NHUCAUDINHDUONGOTRUONG_GETBYKHOILOPID, KhoilopId);
            }
            this.FillGrid_AllSchool(gridViewHienThiPLG, dtNCDD, Tenkhoilop, tenBuaAn, tongSoTre);
            this.FillGridView_AllSchool(gridViewThucPham, gridViewHienThiPLG);
        }
        public DataTable TblBuaantrongngayById(int KhoilopId)
        {
            return ClsTblDm_Sosanhnangluon_dal.TblBuaantrongngayById(KhoilopId);
        }
        public DataTable GetTongsotre(int KhoilopId, int NamhocId, DateTime Ngay)
        {
            return ClsTblDm_Sosanhnangluon_dal.GetTongsotre(QueryString.SPROC_TBLBAOAN_RETURNSUM, KhoilopId, NamhocId, Ngay);
        }
        public void ThanhPhanDD(int idDoTuoi, string Lop, int tongSoTre, GridView gridViewThucPham, GridView gridViewHienThi, string tenBuaAn)
        {
            DataTable dt;
            dt = ClsTblDm_Sosanhnangluon_dal.GetTable(QueryString.PRCTBLDM_SOSANHDINHDUONG_SELECTALL, 3, idDoTuoi, "Quân");
            if (dt != null && dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[0];
                idPhanLoai = int.Parse(dr["IdPhanLoaiLop"].ToString());
                // Lấy Tbl có chứa 1 row duy nhất là nhu cầu dinh dưỡng ở trường.

                DataTable table;
                table = ClsTblDm_Sosanhnangluon_dal.GetTable(QueryString.PRCTBLDM_SOSANHDINHDUONG_SELECTALL, 2, idPhanLoai, "Quân");
                //
                this.FillGrid(gridViewHienThi, table, Lop, tenBuaAn, tongSoTre);
                this.FillGridView(gridViewThucPham, gridViewHienThi);
            }
        }

        public int GetBuaAn(string tenBuaAn)
        {
            DataTable dt;
            dt = ClsTblDm_Sosanhnangluon_dal.GetTable(tenBuaAn);
            if (dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[0];
                return int.Parse(dr["Phanbotienan"].ToString());
            }
            return 0;
        }
        public DataTable SP_Thucphamtheobua_SelectALL(DateTime Ngay, int KhoilopId)
        {
            return ClsTblDm_Sosanhnangluon_dal.SP_Thucphamtheobua_SelectALL(Ngay, KhoilopId);
        }
        private void FillGridView(GridView gridViewInput, GridView gridViewOuput)
        {
            float vCalo;
            float vPro;
            float vLipit;
            float vGlucid;
            if (gridViewInput.Name == "grdToantruong")
            {
                float Calo = 0, Lipit = 0, glucid = 0, Protein = 0, TL = 0, k;
                float TLQD = 0;
                float SL = 0;
                for (int i = 0; i < gridViewInput.RowCount; i++)
                {
                    if (gridViewInput.GetRowCellValue(i, "X_Soluongxuat") != null)
                    {
                        DataTable dtSSNL;
                        QueryString.PRCTBLDM_SOSANHDINHDUONG_SELECTALL = ""
      + "SELECT TblThucpham_thucan.Id AS IdThucPham, TblThucpham_thucan.Tenthucan, iif(isnull(TblThucpham_thucan.Tylequydoi),1,TblThucpham_thucan.Tylequydoi) as Tylequydoi, iif(isnull(TblThucpham_thucan.Tylethai),0, TblThucpham_thucan.Tylethai) as Tylethai,  "
      + "       iif(isnull(TblThucpham_thucan.Calo),0, TblThucpham_thucan.Calo) as Calo, iif(isnull(TblThanhphanhoahoc.Tphh_Protid), 0,TblThanhphanhoahoc.Tphh_Protid) as  Tphh_Protid, iif(isnull(TblThanhphanhoahoc.Tphh_Lipid),0,TblThanhphanhoahoc.Tphh_Lipid) as Tphh_Lipid, iif(isnull(TblThanhphanhoahoc.Tphh_GlucId),0,TblThanhphanhoahoc.Tphh_GlucId) as Tphh_GlucId,  "
      + "       TblDm_Donvitinh.Tendonvitinh "
      + " FROM (TblThanhphanhoahoc INNER JOIN "
      + "       TblThucpham_thucan ON TblThanhphanhoahoc.IdThucpham_thucan = TblThucpham_thucan.Id) INNER JOIN "
      + "       TblDm_Donvitinh ON TblThucpham_thucan.DonvitinhId = TblDm_Donvitinh.Id "
      + " WHERE     (TblThucpham_thucan.Tenthucan = @Name)       ";
                        dtSSNL = ClsTblDm_Sosanhnangluon_dal.GetTable(QueryString.PRCTBLDM_SOSANHDINHDUONG_SELECTALL, 1, 3, gridViewInput.GetRowCellValue(i, "X_Tenthucan").ToString());
                        if (dtSSNL != null && dtSSNL.Rows.Count > 0)
                        {
                            DataRow dr = dtSSNL.Rows[0];
                            TL = float.Parse(dr["Tylethai"].ToString());
                            TLQD = float.Parse(dr["Tylequydoi"].ToString());
                            SL = float.Parse(gridViewInput.GetRowCellValue(i, "X_Soluongxuat").ToString());
                            if (float.TryParse(dr["Calo"].ToString(), out k))
                            {
                                Calo = Calo +
                                (float.Parse(dr["Calo"].ToString()) * (float.Parse(gridViewInput.GetRowCellValue(i, "X_Soluongxuat").ToString()) / TLQD) * 10 * (1 - TL / 100));
                            }
                            if (float.TryParse(dr["Tphh_Protid"].ToString(), out k))
                            {
                                Protein = Protein + (float.Parse(dr["Tphh_Protid"].ToString()) * (float.Parse(gridViewInput.GetRowCellValue(i, "X_Soluongxuat").ToString()) / TLQD) * 10 * (1 - TL / 100));
                            }
                            if (float.TryParse(dr["Tphh_Lipid"].ToString(), out k))
                            {
                                Lipit = Lipit + (float.Parse(dr["Tphh_Lipid"].ToString()) * (float.Parse(gridViewInput.GetRowCellValue(i, "X_Soluongxuat").ToString()) / TLQD) * 10 * (1 - TL / 100));
                            }
                            if (float.TryParse(dr["Tphh_GlucId"].ToString(), out k))
                            {
                                glucid = glucid + (float.Parse(dr["Tphh_GlucId"].ToString()) * (float.Parse(gridViewInput.GetRowCellValue(i, "X_Soluongxuat").ToString()) / TLQD) * 10 * (1 - TL / 100));
                            }
                        }
                    }
                }

                gridViewOuput.SetRowCellValue(1, "tyle", "T.tế");
                gridViewOuput.SetRowCellValue(1, "Kcalootruong", Calo);
                gridViewOuput.SetRowCellValue(1, "ProteinOtruong", Protein);
                gridViewOuput.SetRowCellValue(1, "Lipidotruong", Lipit);
                gridViewOuput.SetRowCellValue(1, "GlucidOtruong", glucid);

                if (gridViewInput.GetRowCellValue(0,"Kcalootruong").ToString() != "0")
                {
                    // tỷ lệ đạt
                    vCalo = (Calo / (float.Parse(gridViewInput.GetRowCellValue(0, "Kcalootruong").ToString()))) * 100;
                    vPro = (Protein / (float.Parse(gridViewInput.GetRowCellValue(0, "ProteinOtruong").ToString()))) * 100;
                    vLipit = (Lipit / (float.Parse(gridViewInput.GetRowCellValue(0, "Lipidotruong").ToString()))) * 100;
                    vGlucid = (glucid / (float.Parse(gridViewInput.GetRowCellValue(0, "GlucidOtruong").ToString()))) * 100;
                    gridViewOuput.SetRowCellValue(2, "tyle", "TLĐ(%)");
                    gridViewOuput.SetRowCellValue(2, "Kcalootruong", Math.Round(vCalo, 2));
                    gridViewOuput.SetRowCellValue(2, "ProteinOtruong", Math.Round(vPro, 2));
                    gridViewOuput.SetRowCellValue(2, "Lipidotruong", Math.Round(vLipit, 2));
                    gridViewOuput.SetRowCellValue(2, "GlucidOtruong", Math.Round(vGlucid, 2));

                    // tỷ lệ plg
                    vPro = (Protein * 4 * 100) / (Protein * 4 + Lipit * 9 + glucid * 4);
                    vLipit = (Lipit * 9 * 100) / (Protein * 4 + Lipit * 9 + glucid * 4);
                    vGlucid = (glucid * 4 * 100) / (Protein * 4 + Lipit * 9 + glucid * 4);
                    try
                    {
                        gridViewOuput.SetRowCellValue(3, "tyle", "P:L:G");
                        gridViewOuput.SetRowCellValue(3, "ProteinOtruong", Math.Round(vPro, 2));
                        gridViewOuput.SetRowCellValue(3, "Lipidotruong", Math.Round(vLipit, 2));
                        gridViewOuput.SetRowCellValue(3, "GlucidOtruong", Math.Round(vGlucid, 2));
                    }
                    catch
                    {

                    }
                }
                else
                {
                    // tỷ lệ đạt
                    gridViewOuput.SetRowCellValue(2, "tyle", "TLĐ(%)");
                    gridViewOuput.SetRowCellValue(2, "Kcalootruong", 0);
                    gridViewOuput.SetRowCellValue(2, "ProteinOtruong", 0);
                    gridViewOuput.SetRowCellValue(2, "Lipidotruong", 0);
                    gridViewOuput.SetRowCellValue(2, "GlucidOtruong", 0);

                    // tỷ lệ plg
                    gridViewOuput.SetRowCellValue(3, "tyle", "P:L:G");
                    gridViewOuput.SetRowCellValue(3, "ProteinOtruong", 0);
                    gridViewOuput.SetRowCellValue(3, "Lipidotruong", 0);
                    gridViewOuput.SetRowCellValue(3, "GlucidOtruong", 0);
                }

            }
            else
            {
                // thomnd code mới 12.04.2011

                ////1. Đổi đơn vị khác Kilogam về Kilogam.
                // 2. Đổi Kilogam về gam.
                // 3. tính tỷ lệ đạt theo công thức:
                /*
                 */
                float Calo = 0, Lipit = 0, glucid = 0, Protein = 0, TL = 0, k;
                float TLQD = 0;
                float SL = 0;
                for (int i = 0; i < gridViewInput.RowCount; i++)
                {
                    if (gridViewInput.GetRowCellValue(i, "Tenthucan").ToString() != null)
                    {
                        DataTable dtSSNL;
                        QueryString.PRCTBLDM_SOSANHDINHDUONG_SELECTALL = ""
      + "SELECT TblThucpham_thucan.Id AS IdThucPham, TblThucpham_thucan.Tenthucan, iif(isnull(TblThucpham_thucan.Tylequydoi),1,TblThucpham_thucan.Tylequydoi) as Tylequydoi, iif(isnull(TblThucpham_thucan.Tylethai),0, TblThucpham_thucan.Tylethai) as Tylethai,  "
      + "       iif(isnull(TblThucpham_thucan.Calo),0, TblThucpham_thucan.Calo) as Calo, iif(isnull(TblThanhphanhoahoc.Tphh_Protid), 0,TblThanhphanhoahoc.Tphh_Protid) as  Tphh_Protid, iif(isnull(TblThanhphanhoahoc.Tphh_Lipid),0,TblThanhphanhoahoc.Tphh_Lipid) as Tphh_Lipid, iif(isnull(TblThanhphanhoahoc.Tphh_GlucId),0,TblThanhphanhoahoc.Tphh_GlucId) as Tphh_GlucId,  "
      + "       TblDm_Donvitinh.Tendonvitinh "
      + " FROM (TblThanhphanhoahoc INNER JOIN "
      + "       TblThucpham_thucan ON TblThanhphanhoahoc.IdThucpham_thucan = TblThucpham_thucan.Id) INNER JOIN "
      + "       TblDm_Donvitinh ON TblThucpham_thucan.DonvitinhId = TblDm_Donvitinh.Id "
      + " WHERE     (TblThucpham_thucan.Tenthucan = @Name)       ";
                        dtSSNL = ClsTblDm_Sosanhnangluon_dal.GetTable(QueryString.PRCTBLDM_SOSANHDINHDUONG_SELECTALL, 1, 3, gridViewInput.GetRowCellValue(i, "Tenthucan").ToString());
                        if (dtSSNL != null && dtSSNL.Rows.Count > 0)
                        {
                            DataRow dr = dtSSNL.Rows[0];
                            TL = float.Parse(dr["Tylethai"].ToString());
                            TLQD = float.Parse(dr["Tylequydoi"].ToString());
                            SL = float.Parse(gridViewInput.GetRowCellValue(i, "Soluongxuat").ToString());
                            if (float.TryParse(dr["Calo"].ToString(), out k))
                            {
                                Calo = Calo +
                                (float.Parse(dr["Calo"].ToString()) * (float.Parse(gridViewInput.GetRowCellValue(i, "Soluongxuat").ToString()) / TLQD) * 10 * (1 - TL / 100));
                            }
                            if (float.TryParse(dr["Tphh_Protid"].ToString(), out k))
                            {
                                Protein = Protein + (float.Parse(dr["Tphh_Protid"].ToString()) * (float.Parse(gridViewInput.GetRowCellValue(i, "Soluongxuat").ToString()) / TLQD) * 10 * (1 - TL / 100));
                            }
                            if (float.TryParse(dr["Tphh_Lipid"].ToString(), out k))
                            {
                                Lipit = Lipit + (float.Parse(dr["Tphh_Lipid"].ToString()) * (float.Parse(gridViewInput.GetRowCellValue(i, "Soluongxuat").ToString()) / TLQD) * 10 * (1 - TL / 100));
                            }
                            if (float.TryParse(dr["Tphh_GlucId"].ToString(), out k))
                            {
                                glucid = glucid + (float.Parse(dr["Tphh_GlucId"].ToString()) * (float.Parse(gridViewInput.GetRowCellValue(i, "Soluongxuat").ToString()) / TLQD) * 10 * (1 - TL / 100));
                            }
                        }
                    }
                }
                // có thể chuyển sang table để add giá trị và add các row
                //((DataTable)gridViewOuput.GridControl.DataSource).Rows.
                gridViewOuput.SetRowCellValue(1, "tyle", "T.tế");
                gridViewOuput.SetRowCellValue(1, "Kcalootruong", Calo);
                gridViewOuput.SetRowCellValue(1, "ProteinOtruong", Protein);
                gridViewOuput.SetRowCellValue(1, "Lipidotruong", Lipit);
                gridViewOuput.SetRowCellValue(1, "GlucidOtruong", glucid);

                if (gridViewOuput.GetRowCellValue(0, "Kcalootruong") != null && gridViewOuput.GetRowCellValue(0, "Kcalootruong").ToString() != "0")
                {
                    // tỷ lệ đạt
                    vCalo = (Calo / (float.Parse(gridViewOuput.GetRowCellValue(0, "Kcalootruong").ToString()))) * 100;
                    vPro = (Protein / (float.Parse(gridViewOuput.GetRowCellValue(0, "ProteinOtruong").ToString()))) * 100;
                    vLipit = (Lipit / (float.Parse(gridViewOuput.GetRowCellValue(0, "Lipidotruong").ToString()))) * 100;
                    vGlucid = (glucid / (float.Parse(gridViewOuput.GetRowCellValue(0, "GlucidOtruong").ToString()))) * 100;
                    gridViewOuput.SetRowCellValue(2, "tyle", "TLĐ(%)");
                    gridViewOuput.SetRowCellValue(2, "Kcalootruong", Math.Round(vCalo, 2));
                    gridViewOuput.SetRowCellValue(2, "ProteinOtruong", Math.Round(vPro, 2));
                    gridViewOuput.SetRowCellValue(2, "Lipidotruong", Math.Round(vLipit, 2));
                    gridViewOuput.SetRowCellValue(2, "GlucidOtruong", Math.Round(vGlucid, 2));

                    // tỷ lệ plg
                    vPro = (Protein * 4 * 100) / (Protein * 4 + Lipit * 9 + glucid * 4);
                    vLipit = (Lipit * 9 * 100) / (Protein * 4 + Lipit * 9 + glucid * 4);
                    vGlucid = (glucid * 4 * 100) / (Protein * 4 + Lipit * 9 + glucid * 4);
                    try
                    {
                        gridViewOuput.SetRowCellValue(3, "tyle", "P:L:G");
                        gridViewOuput.SetRowCellValue(3, "ProteinOtruong", Math.Round(vPro, 2));
                        gridViewOuput.SetRowCellValue(3, "Lipidotruong", Math.Round(vLipit, 2));
                        gridViewOuput.SetRowCellValue(3, "GlucidOtruong", Math.Round(vGlucid, 2));
                    }
                    catch
                    {

                    }
                }
                else
                {
                    // tỷ lệ đạt
                    gridViewOuput.SetRowCellValue(2, "tyle", "TLĐ(%)");
                    gridViewOuput.SetRowCellValue(2, "Kcalootruong", 0);
                    gridViewOuput.SetRowCellValue(2, "ProteinOtruong", 0);
                    gridViewOuput.SetRowCellValue(2, "Lipidotruong", 0);
                    gridViewOuput.SetRowCellValue(2, "GlucidOtruong", 0);

                    // tỷ lệ plg
                    gridViewOuput.SetRowCellValue(3, "tyle", "P:L:G");
                    gridViewOuput.SetRowCellValue(3, "ProteinOtruong", 0);
                    gridViewOuput.SetRowCellValue(3, "Lipidotruong", 0);
                    gridViewOuput.SetRowCellValue(3, "GlucidOtruong", 0);
                }
            }
        }

        private void FillGridView_AllSchool(GridView gridViewInput, GridView gridViewOuput)
        {
            if (gridViewInput.Name == "grdToantruong")
            {
                float Calo = 0, Lipit = 0, glucid = 0, Protein = 0, TL = 0, k;
                float TLQD = 0;
                float SL = 0;
                for (int i = 0; i < gridViewInput.RowCount; i++)
                {
                    if (gridViewInput.GetRowCellValue(i, "X_Soluongxuat") != null)
                    {
                        DataTable dtSSNL;
                        QueryString.PRCTBLDM_SOSANHDINHDUONG_SELECTALL = ""
      + "SELECT TblThucpham_thucan.Id AS IdThucPham, TblThucpham_thucan.Tenthucan, iif(isnull(TblThucpham_thucan.Tylequydoi),1,TblThucpham_thucan.Tylequydoi) as Tylequydoi, iif(isnull(TblThucpham_thucan.Tylethai),0, TblThucpham_thucan.Tylethai) as Tylethai,  "
      + "       iif(isnull(TblThucpham_thucan.Calo),0, TblThucpham_thucan.Calo) as Calo, iif(isnull(TblThanhphanhoahoc.Tphh_Protid), 0,TblThanhphanhoahoc.Tphh_Protid) as  Tphh_Protid, iif(isnull(TblThanhphanhoahoc.Tphh_Lipid),0,TblThanhphanhoahoc.Tphh_Lipid) as Tphh_Lipid, iif(isnull(TblThanhphanhoahoc.Tphh_GlucId),0,TblThanhphanhoahoc.Tphh_GlucId) as Tphh_GlucId,  "
      + "       TblDm_Donvitinh.Tendonvitinh "
      + " FROM (TblThanhphanhoahoc INNER JOIN "
      + "       TblThucpham_thucan ON TblThanhphanhoahoc.IdThucpham_thucan = TblThucpham_thucan.Id) INNER JOIN "
      + "       TblDm_Donvitinh ON TblThucpham_thucan.DonvitinhId = TblDm_Donvitinh.Id "
      + " WHERE     (TblThucpham_thucan.Tenthucan = @Name)       ";
                        dtSSNL = ClsTblDm_Sosanhnangluon_dal.GetTable(QueryString.PRCTBLDM_SOSANHDINHDUONG_SELECTALL, 1, 3, gridViewInput.GetRowCellValue(i, "X_Tenthucan").ToString());
                        if (dtSSNL != null && dtSSNL.Rows.Count > 0)
                        {
                            DataRow dr = dtSSNL.Rows[0];
                            TL = float.Parse(dr["Tylethai"].ToString());
                            TLQD = float.Parse(dr["Tylequydoi"].ToString());
                            SL = float.Parse(gridViewInput.GetRowCellValue(i, "X_Soluongxuat").ToString());
                            if (float.TryParse(dr["Calo"].ToString(), out k))
                            {
                                Calo = Calo +
                                (float.Parse(dr["Calo"].ToString()) * (float.Parse(gridViewInput.GetRowCellValue(i, "X_Soluongxuat").ToString()) / TLQD) * 10 * (1 - TL / 100));
                            }
                            if (float.TryParse(dr["Tphh_Protid"].ToString(), out k))
                            {
                                Protein = Protein + (float.Parse(dr["Tphh_Protid"].ToString()) * (float.Parse(gridViewInput.GetRowCellValue(i, "X_Soluongxuat").ToString()) / TLQD) * 10 * (1 - TL / 100));
                            }
                            if (float.TryParse(dr["Tphh_Lipid"].ToString(), out k))
                            {
                                Lipit = Lipit + (float.Parse(dr["Tphh_Lipid"].ToString()) * (float.Parse(gridViewInput.GetRowCellValue(i, "X_Soluongxuat").ToString()) / TLQD) * 10 * (1 - TL / 100));
                            }
                            if (float.TryParse(dr["Tphh_GlucId"].ToString(), out k))
                            {
                                glucid = glucid + (float.Parse(dr["Tphh_GlucId"].ToString()) * (float.Parse(gridViewInput.GetRowCellValue(i, "X_Soluongxuat").ToString()) / TLQD) * 10 * (1 - TL / 100));
                            }
                        }
                    }
                }

                gridViewOuput.SetRowCellValue(1, "tyle", "T.tế");
                gridViewOuput.SetRowCellValue(1, "Kcalootruong", Calo);
                gridViewOuput.SetRowCellValue(1, "ProteinOtruong", Protein);
                gridViewOuput.SetRowCellValue(1, "Lipidotruong", Lipit);
                gridViewOuput.SetRowCellValue(1, "GlucidOtruong", glucid);

                if (gridViewOuput.GetRowCellValue(0, "Kcalootruong").ToString() != "0")
                {
                    // tỷ lệ đạt
                    gridViewOuput.SetRowCellValue(2, "tyle", "TLĐ(%)");
                    gridViewOuput.SetRowCellValue(2, "Kcalootruong", (Calo / (float.Parse(gridViewOuput.GetRowCellValue(0, "Kcalootruong").ToString()))) * 100);
                    gridViewOuput.SetRowCellValue(2, "ProteinOtruong", (Protein / (float.Parse(gridViewOuput.GetRowCellValue(0, "ProteinOtruong").ToString()))) * 100);
                    gridViewOuput.SetRowCellValue(2, "Lipidotruong", (Lipit / (float.Parse(gridViewOuput.GetRowCellValue(0, "Lipidotruong").ToString()))) * 100);
                    gridViewOuput.SetRowCellValue(2, "GlucidOtruong", (glucid / (float.Parse(gridViewOuput.GetRowCellValue(0, "GlucidOtruong").ToString()))) * 100);

                    // tỷ lệ plg
                    try
                    {

                        gridViewOuput.SetRowCellValue(3, "tyle", "P:L:G");
                        gridViewOuput.SetRowCellValue(3, "ProteinOtruong", (Protein * 4 * 100) / (Protein * 4 + Lipit * 9 + glucid * 4));
                        gridViewOuput.SetRowCellValue(3, "Lipidotruong", (Lipit * 9 * 100) / (Protein * 4 + Lipit * 9 + glucid * 4));
                        gridViewOuput.SetRowCellValue(3, "GlucidOtruong", (glucid * 4 * 100) / (Protein * 4 + Lipit * 9 + glucid * 4));
                    }
                    catch
                    {

                    }
                }
                else
                {
                    // tỷ lệ đạt
                    gridViewOuput.SetRowCellValue(2, "tyle", "TLĐ(%)");
                    gridViewOuput.SetRowCellValue(2, "Kcalootruong", 0);
                    gridViewOuput.SetRowCellValue(2, "ProteinOtruong", 0);
                    gridViewOuput.SetRowCellValue(2, "Lipidotruong", 0);
                    gridViewOuput.SetRowCellValue(2, "GlucidOtruong", 0);

                    // tỷ lệ plg
                    gridViewOuput.SetRowCellValue(3, "tyle", "P:L:G");
                    gridViewOuput.SetRowCellValue(3, "ProteinOtruong", 0);
                    gridViewOuput.SetRowCellValue(3, "Lipidotruong", 0);
                    gridViewOuput.SetRowCellValue(3, "GlucidOtruong", 0);
                }

            }
            else
            {
                // thomnd code mới 12.04.2011

                ////1. Đổi đơn vị khác Kilogam về Kilogam.
                // 2. Đổi Kilogam về gam.
                // 3. tính tỷ lệ đạt theo công thức:
                /*
                 */
                float Calo = 0, Lipit = 0, glucid = 0, Protein = 0, TL = 0, k;
                float TLQD = 0;
                float SL = 0;
                for (int i = 0; i < gridViewInput.RowCount; i++)
                {
                    if (gridViewInput.GetRowCellValue(i, "dataGridViewTextBoxColumn3") != null)
                    {
                        DataTable dtSSNL;
                        QueryString.PRCTBLDM_SOSANHDINHDUONG_SELECTALL = ""
      + "SELECT TblThucpham_thucan.Id AS IdThucPham, TblThucpham_thucan.Tenthucan, iif(isnull(TblThucpham_thucan.Tylequydoi),1,TblThucpham_thucan.Tylequydoi) as Tylequydoi, iif(isnull(TblThucpham_thucan.Tylethai),0, TblThucpham_thucan.Tylethai) as Tylethai,  "
      + "       iif(isnull(TblThucpham_thucan.Calo),0, TblThucpham_thucan.Calo) as Calo, iif(isnull(TblThanhphanhoahoc.Tphh_Protid), 0,TblThanhphanhoahoc.Tphh_Protid) as  Tphh_Protid, iif(isnull(TblThanhphanhoahoc.Tphh_Lipid),0,TblThanhphanhoahoc.Tphh_Lipid) as Tphh_Lipid, iif(isnull(TblThanhphanhoahoc.Tphh_GlucId),0,TblThanhphanhoahoc.Tphh_GlucId) as Tphh_GlucId,  "
      + "       TblDm_Donvitinh.Tendonvitinh "
      + " FROM (TblThanhphanhoahoc INNER JOIN "
      + "       TblThucpham_thucan ON TblThanhphanhoahoc.IdThucpham_thucan = TblThucpham_thucan.Id) INNER JOIN "
      + "       TblDm_Donvitinh ON TblThucpham_thucan.DonvitinhId = TblDm_Donvitinh.Id "
      + " WHERE     (TblThucpham_thucan.Tenthucan = @Name)       ";
                        dtSSNL = ClsTblDm_Sosanhnangluon_dal.GetTable(QueryString.PRCTBLDM_SOSANHDINHDUONG_SELECTALL, 1, 3, gridViewInput.GetRowCellValue(i, "dataGridViewTextBoxColumn3").ToString());
                        
                        if (dtSSNL != null && dtSSNL.Rows.Count > 0)
                        {
                            DataRow dr = dtSSNL.Rows[0];
                            TL = float.Parse(dr["Tylethai"].ToString());
                            TLQD = float.Parse(dr["Tylequydoi"].ToString());
                            SL = float.Parse(gridViewInput.GetRowCellValue(i, "dataGridViewTextBoxColumn14").ToString());
                            if (float.TryParse(dr["Calo"].ToString(), out k))
                            {
                                Calo = Calo +
                                (float.Parse(dr["Calo"].ToString()) * (float.Parse(gridViewInput.GetRowCellValue(i, "dataGridViewTextBoxColumn14").ToString()) / TLQD) * 10 * (1 - TL / 100));
                            }
                            if (float.TryParse(dr["Tphh_Protid"].ToString(), out k))
                            {
                                Protein = Protein + (float.Parse(dr["Tphh_Protid"].ToString()) * (float.Parse(gridViewInput.GetRowCellValue(i, "dataGridViewTextBoxColumn14").ToString()) / TLQD) * 10 * (1 - TL / 100));
                            }
                            if (float.TryParse(dr["Tphh_Lipid"].ToString(), out k))
                            {
                                Lipit = Lipit + (float.Parse(dr["Tphh_Lipid"].ToString()) * (float.Parse(gridViewInput.GetRowCellValue(i, "dataGridViewTextBoxColumn14").ToString()) / TLQD) * 10 * (1 - TL / 100));
                            }
                            if (float.TryParse(dr["Tphh_GlucId"].ToString(), out k))
                            {
                                glucid = glucid + (float.Parse(dr["Tphh_GlucId"].ToString()) * (float.Parse(gridViewInput.GetRowCellValue(i, "dataGridViewTextBoxColumn14").ToString()) / TLQD) * 10 * (1 - TL / 100));
                            }
                        }
                    }
                }

                gridViewOuput.SetRowCellValue(1, "tyle", "T.tế");
                gridViewOuput.SetRowCellValue(1, "Kcalootruong", Calo);
                gridViewOuput.SetRowCellValue(1, "ProteinOtruong", Protein);
                gridViewOuput.SetRowCellValue(1, "Lipidotruong", Lipit);
                gridViewOuput.SetRowCellValue(1, "GlucidOtruong", glucid);

                if (gridViewOuput.GetRowCellValue(0, "Kcalootruong").ToString() != "0")
                {
                    // tỷ lệ đạt
                    gridViewOuput.SetRowCellValue(2, "tyle", "TLĐ(%)");
                    gridViewOuput.SetRowCellValue(2, "Kcalootruong", (Calo / (float.Parse(gridViewOuput.GetRowCellValue(0, "Kcalootruong").ToString()))) * 100);
                    gridViewOuput.SetRowCellValue(2, "ProteinOtruong", (Protein / (float.Parse(gridViewOuput.GetRowCellValue(0, "ProteinOtruong").ToString()))) * 100);
                    gridViewOuput.SetRowCellValue(2, "Lipidotruong", (Lipit / (float.Parse(gridViewOuput.GetRowCellValue(0, "Lipidotruong").ToString()))) * 100);
                    gridViewOuput.SetRowCellValue(2, "GlucidOtruong", (glucid / (float.Parse(gridViewOuput.GetRowCellValue(0, "GlucidOtruong").ToString()))) * 100);

                    // tỷ lệ plg
                    try
                    {
                        gridViewOuput.SetRowCellValue(3, "tyle", "P:L:G");
                        gridViewOuput.SetRowCellValue(3, "ProteinOtruong", (Protein * 4 * 100) / (Protein * 4 + Lipit * 9 + glucid * 4));
                        gridViewOuput.SetRowCellValue(3, "Lipidotruong", (Lipit * 9 * 100) / (Protein * 4 + Lipit * 9 + glucid * 4));
                        gridViewOuput.SetRowCellValue(3, "GlucidOtruong", (glucid * 4 * 100) / (Protein * 4 + Lipit * 9 + glucid * 4));
                    }
                    catch
                    {

                    }
                }
                else
                {
                    // tỷ lệ đạt
                    gridViewOuput.SetRowCellValue(2, "tyle", "TLĐ(%)");
                    gridViewOuput.SetRowCellValue(2, "Kcalootruong", 0);
                    gridViewOuput.SetRowCellValue(2, "ProteinOtruong", 0);
                    gridViewOuput.SetRowCellValue(2, "Lipidotruong", 0);
                    gridViewOuput.SetRowCellValue(2, "GlucidOtruong", 0);

                    // tỷ lệ plg
                    gridViewOuput.SetRowCellValue(3, "tyle", "P:L:G");
                    gridViewOuput.SetRowCellValue(3, "ProteinOtruong", 0);
                    gridViewOuput.SetRowCellValue(3, "Lipidotruong", 0);
                    gridViewOuput.SetRowCellValue(3, "GlucidOtruong", 0);
                }
            }
        }

        public void GridPaiting(object sender, DataGridViewCellPaintingEventArgs e, DataGridView gridView, int fromCell, int toCell)
        {
            if (e.RowIndex == 1)
            {
                if (e.ColumnIndex == 1)
                {
                    e.PaintBackground(e.ClipBounds, true);
                    Rectangle r = e.CellBounds;
                    Rectangle r1 = gridView.GetCellDisplayRectangle(2, 1, true);
                    r.Width += r1.Width - 1;
                    r.Height -= 1;
                    using (SolidBrush brBk = new SolidBrush(e.CellStyle.BackColor))
                    using (SolidBrush brFr = new SolidBrush(e.CellStyle.ForeColor))
                    {
                        e.Graphics.FillRectangle(brBk, r);
                        StringFormat sf = new StringFormat();
                        sf.Alignment = StringAlignment.Center;
                        sf.LineAlignment = StringAlignment.Center;
                        //  e.Graphics.DrawString("cell merged", e.CellStyle.Font, brFr, r, sf);
                    }
                    e.Handled = true;
                }
                if (e.ColumnIndex == 2)
                {
                    using (Pen p = new Pen(gridView.GridColor))
                    {
                        e.Graphics.DrawLine(p, e.CellBounds.Left, e.CellBounds.Bottom - 1,
                        e.CellBounds.Right, e.CellBounds.Bottom - 1);
                        e.Graphics.DrawLine(p, e.CellBounds.Right - 1, e.CellBounds.Top,
                        e.CellBounds.Right - 1, e.CellBounds.Bottom);
                    }
                    e.Handled = true;
                }
            }
        }
    }   
}
